import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;

import javax.xml.parsers.ParserConfigurationException;

import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.DomSerializer;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.PrettyXmlSerializer;
import org.htmlcleaner.TagNode;
import org.jsoup.Jsoup;


public class HTMLManager {
	String url;

	ArrayList<String> usableTagNames = new ArrayList<String>();
	ArrayList<TagNode> usableText = new ArrayList<TagNode>();

	
	
	public HTMLManager(String url){
		this.url = url;
		usableTagNames.add("title");
		//usableTagNames.add("meta");
		//usableTagNames.add("link");
		//usableTagNames.add("a");
		usableTagNames.add("p");

		
		
	}

	private boolean isUsableTagName(String tagName){
		for(String usableTagName: usableTagNames){
			if(tagName.equals(usableTagName)){
				return true;
			}
		}
		return false;
		
	}
	
	public String getHTML() throws IOException{
		CleanerProperties props = new CleanerProperties();
		 
		// set some properties to non-default values
		props.setTranslateSpecialEntities(true);
		props.setTransResCharsToNCR(true);
		props.setOmitComments(true);
		
		
		
		TagNode tagNode = new HtmlCleaner(props).clean(
			    new URL(url)
		);
		
		//Output to file
		

		new PrettyXmlSerializer(props).writeToFile(
			    tagNode, "testBanana.xml", "utf-8"
			);

		ParseNode2(new ArrayList<String>(),tagNode);
		
		
		 return null;
	}
	
	public void processUsableText(){
		System.out.println("Printing Usable Tags:");
		System.out.println();
		for(TagNode tagNode:usableText){
			System.out.println();
			System.out.println("****************************************");
			System.out.println("tag: " + tagNode.getName());
			
			Map<String,String> attributes = tagNode.getAttributes();
			Set<String> keySet = attributes.keySet();
			
			for(String key: keySet){
				System.out.println("attribute: " + key + " : " + attributes.get(key));
			}
			
			String[] textWords = tagNode.getText().toString().split("\\s+");
			System.out.println("Words:");
			System.out.println();
			for(String word:textWords){
				System.out.println(word);
			}
		}
		
	}
	
	private void ParseNode(ArrayList<String> superTags, TagNode tagNode){
		ArrayList<String> tagsList = new ArrayList<String>();
		for( String tag: superTags){
			tagsList.add(tag);
		}
		TagNode[] tags = tagNode.getChildTags();

		if(tags.length == 0){
			System.out.println();
			System.out.println("tag: " + tagNode.getName());
			System.out.println("name: " + tagNode.getText());
		}
		tagsList.add(tagNode.getName());
		
		for(int i =0;i< tags.length;i++){
			ParseNode(tagsList,tags[i]);
		}
		
	}
	
	private void ParseNode2(ArrayList<String> superTags, TagNode tagNode){
		if(this.isUsableTagName(tagNode.getName())){
			this.foundUsableTag(superTags,tagNode);
			return;
		}
		
		ArrayList<String> tagsList = new ArrayList<String>();
		for( String tag: superTags){
			tagsList.add(tag);
		}
		TagNode[] tags = tagNode.getChildTags();

		tagsList.add(tagNode.getName());
		
		for(int i =0;i< tags.length;i++){
			ParseNode2(tagsList,tags[i]);
		}
		
	}
	
	private static String stringList(ArrayList<String> toPrint){
		String toReturn = "";
		for(String a:toPrint){
			toReturn += a + " ";
		}
		return toReturn;
	}
	
	private void foundUsableTag(ArrayList<String> superTags, TagNode tagNode){
		
		usableText.add(tagNode);
		/*
		System.out.println("tag: " + tagNode.getName());
		System.out.println("super tags: " + stringList(superTags));
		
		Map<String,String> attributes = tagNode.getAttributes();
		Set<String> keySet = attributes.keySet();
		
		for(String key: keySet){
			System.out.println("attribute: " + key + " : " + attributes.get(key));
		}
		
		System.out.println("text: " + tagNode.getText());
		
		System.out.println();
		*/
	}

}
